package jude.java.money.article;

import org.apache.commons.lang.StringUtils;


public class ArticleSite {

	public static final int PAGE_MODE_NORMAL = 0;		//普通, page=1, page=2
	public static final int PAGE_MODE_SOAP = 1;			//重写, /page/1, /page/2
	public static final int PAGE_MODE_RECORD = 2;		//记录, start=10, start=20
	
	public static final String DEFAULT_PATH_TEMPLATE = "{BASE}\\{SITE}\\{MONTH}\\{DAY}\\{TITLE}.txt";
	public static final String DEFAULT_QUERY_PATH_TEMPLATE = "{BASE}\\{SITE}\\{QUERY}\\{TITLE}.txt";
	
	private String name;
	private String pageKey;
	private String queryKey;
	private String query;
	
	private Page listPage;
	private Page queryPage;
	private Page contentPage;
	
	private String charset;
	private String shieldString;
	
	private ContentHandler titleHandler;
	private ContentHandler contentHandler;
	
	private int startPage = 1;
	private int skipCount = 0;
	private int pageCount = 0;
	private int interval = 3000;
	private int pageMode = PAGE_MODE_NORMAL;
	private boolean http403 = false;
	private int pageSize = 10;
	private String pathTemplate = DEFAULT_PATH_TEMPLATE;
	
	public ArticleSite() {
	}
	public ArticleSite(String name) {
		this.name = name;
	}

	public ArticleSite(String name, String pageKey, String queryKey) {
		this.name = name;
		this.pageKey= pageKey;
		this.queryKey = queryKey;
	}
	public ArticleSite(String name, String pageKey, String queryKey, Page... pages) {
		this(name, pageKey, queryKey);
		if (pages.length > 0)
			this.listPage = pages[0];
		if (pages.length > 1)
			this.queryPage = pages[1];
		if (pages.length > 2)
			this.contentPage = pages[2];
	}
	
	public int getInterval() {
		return interval;
	}
	public ArticleSite setInterval(int interval) {
		this.interval = interval;
		return this;
	}
	public String getShieldString() {
		return shieldString;
	}
	public ArticleSite setShieldString(String shieldString) {
		this.shieldString = shieldString;
		return this;
	}
	
	public boolean listable() {
		return listPage != null;
	}
	
	public boolean queryable() {
		return queryKey != null && queryPage != null && StringUtils.isNotEmpty(query);
	}
	
	public ArticleSite setListPage(Page listPage) {
		this.listPage = listPage;
		return this;
	}
	public Page getListPage() {
		return listPage;
	}
	public ArticleSite setQueryPage(Page queryPage) {
		this.queryPage = queryPage;
		return this;
	}
	public Page getQueryPage() {
		return queryPage;
	}
	public ArticleSite setContentPage(Page contentPage) {
		this.contentPage = contentPage;
		return this;
	}
	public Page getContentPage() {
		return contentPage;
	}
	
	public String getName() {
		return name;
	}
	public ArticleSite setName(String name) {
		this.name = name;
		return this;
	}
	public String getPageKey() {
		return pageKey;
	}
	public ArticleSite setPageKey(String pageKey) {
		this.pageKey = pageKey;
		return this;
	}
	public String getQueryKey() {
		return queryKey;
	}
	public ArticleSite setQueryKey(String queryKey) {
		this.queryKey = queryKey;
		return this;
	}
	public String getQuery() {
		return query;
	}
	public ArticleSite setQuery(String query) {
		this.query = query;
		return this;
	}
	public int getPageMode() {
		return pageMode;
	}
	public ArticleSite setPageMode(int pageMode) {
		this.pageMode = pageMode;
		return this;
	}
	public boolean isHttp403() {
		return http403;
	}
	public ArticleSite setHttp403(boolean http403) {
		this.http403 = http403;
		return this;
	}
	public int getPageSize() {
		return pageSize;
	}
	public ArticleSite setPageSize(int pageSize) {
		this.pageSize = pageSize;
		return this;
	}
	public int getStartPage() {
		return startPage;
	}
	public ArticleSite setStartPage(int startPage) {
		this.startPage = startPage;
		return this;
	}
	public int getPageCount() {
		return pageCount;
	}
	public ArticleSite setPageCount(int pageCount) {
		this.pageCount = pageCount;
		return this;
	}
	public ArticleSite setEndPage(int endPage) {
		this.pageCount = endPage - startPage;
		return this;
	}
	public String getCharset() {
		return charset;
	}
	public ArticleSite setCharset(String charset) {
		this.charset = charset;
		return this;
	}
	public String getPathTemplate() {
		return pathTemplate;
	}
	public ArticleSite setPathTemplate(String pathTemplate) {
		this.pathTemplate = pathTemplate;
		return this;
	}
	public ContentHandler getContentHandler() {
		return contentHandler;
	}
	public ArticleSite setContentHandler(ContentHandler contentHandler) {
		this.contentHandler = contentHandler;
		return this;
	}
	public ContentHandler getTitleHandler() {
		return titleHandler;
	}
	public ArticleSite setTitleHandler(ContentHandler titleHandler) {
		this.titleHandler = titleHandler;
		return this;
	}
	public ArticleSite setSkipCount(int skipCount) {
		this.skipCount = skipCount;
		return this;
	}
	public int getSkipCount() {
		return skipCount;
	}
	
	public interface ContentHandler {
		public String handler(String content);
	}
	
}
